@@ -44,6 +44,46 @@ public function ajax(array|string $attributes = ''): static
4444 return $ this ;
4545 }
4646
47+ /**
48+ * @param string $url
49+ * @param string $formSelector
50+ * @return $this
51+ */
52+ public function postAjaxWithForm (string $ url , string $ formSelector ): static
53+ {
54+ $ attributes = ['url ' => $ url ];
55+
56+ Arr::set ($ attributes , 'type ' , 'POST ' );
57+ Arr::set ($ attributes , 'headers.X-HTTP-Method-Override ' , 'GET ' );
58+
59+ $ script = $ this ->getScriptWithFormSelector ($ formSelector );
60+
61+ $ attributes ['data ' ] = "function(data) { $ script } " ;
62+
63+ return $ this ->ajax ($ attributes );
64+ }
65+
66+ /**
67+ * @param string $formSelector
68+ * @return string
69+ */
70+ protected function getScriptWithFormSelector (string $ formSelector ): string
71+ {
72+ return <<<CDATA
73+ var formData = _.groupBy($(" $ formSelector").find("input, select").serializeArray(), function(d) { return d.name; } );
74+ $.each(formData, function(i, group){
75+ if (group.length > 1) {
76+ data[group[0].name] = [];
77+ $.each(group, function(i, obj) {
78+ data[obj.name].push(obj.value)
79+ })
80+ } else {
81+ data[group[0].name] = group[0].value;
82+ }
83+ });
84+ CDATA ;
85+ }
86+
4787 /**
4888 * Setup ajax parameter for datatables pipeline plugin.
4989 *
@@ -80,14 +120,7 @@ public function getAjaxUrl(): string
80120 */
81121 public function ajaxWithForm (string $ url , string $ formSelector ): static
82122 {
83- $ script = <<<CDATA
84- var formData = $(" $ formSelector").find("input, select").serializeArray();
85- $.each(formData, function(i, obj){
86- data[obj.name] = obj.value;
87- });
88- CDATA ;
89-
90- return $ this ->minifiedAjax ($ url , $ script );
123+ return $ this ->minifiedAjax ($ url , $ this ->getScriptWithFormSelector ($ formSelector ));
91124 }
92125
93126 /**
0 commit comments